package mysql

import (
	"news/models"

	"go.uber.org/zap"
)

func CreateComment(p *models.Comment) (err error) {
	sqlStr := `insert into 
				comment(comment_id, content, post_id, author_id, parent_id,status) 
				values (?,?,?,?,?,?)`
	_, err = db.Exec(sqlStr, p.CommentID, p.Content, p.PostID,
		p.AuthorId, p.ParentID, p.Status)
	if err != nil {
		zap.L().Error("insert comment failed",
			zap.String("sqlstr:", sqlStr), zap.Error(err))
		return
	}

	return
}

// 获取评论的 所有列表信息
func GetCommentList(page, size int64) (data []*models.Comment, err error) {
	sqlStr := "select comment_id, content, post_id, author_id," +
		" parent_id, status,create_time from comment limit ? ,?"
	err = db.Select(&data, sqlStr, (page-1)*size, size)
	if err != nil {
		zap.L().Error("GetCommentList failed",
			zap.String("sqlStr:", sqlStr),
			zap.Error(err))
		return nil, err
	}
	return

}
